//
//  utils.m
//  HZHome
//
//  Created by SunShine on 13-9-20.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "utils.h"
#import "NSObject+propertyList.h"
#import "DataStoreService.h"
#import "MKNetworkKit.h"
#import "UserInfoService.h"
#import <CommonCrypto/CommonDigest.h>
//生成一个自定义的导航栏返回按钮
UIBarButtonItem *getCustomNavBackButton(id iTarget, SEL iSelector)
{
    UIButton *aCustomView = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"back_btn_normal"];
    [aCustomView setImage:image forState:UIControlStateNormal];
    aCustomView.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
    [aCustomView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aCustomView addTarget:iTarget action:iSelector forControlEvents:UIControlEventTouchUpInside];
    //将按钮向左偏移10个像素
//    aCustomView.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    return [[UIBarButtonItem alloc] initWithCustomView:aCustomView];
}

UIBarButtonItem *customNavBackButton(id iTarget, SEL iSelector)
{
    UIButton *aCustomView = [UIButton buttonWithType:UIButtonTypeCustom];
    [aCustomView setImage:[UIImage imageNamed:@"back_btn_normal"] forState:UIControlStateNormal];
    aCustomView.frame = CGRectMake(0, 0, 18, 18);
    [aCustomView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aCustomView addTarget:iTarget action:iSelector forControlEvents:UIControlEventTouchUpInside];
    //将按钮向左偏移10个像素
    return [[UIBarButtonItem alloc] initWithCustomView:aCustomView];
}

UIBarButtonItem *autoavBackButton(id iTarget, SEL iSelector)
{
    UIButton *aCustomView = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"back_btn_normal"];
    [aCustomView setImage:image forState:UIControlStateNormal];
    aCustomView.frame = CGRectMake(0, 0, image.size.width / 2, image.size.height / 2);
    [aCustomView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aCustomView addTarget:iTarget action:iSelector forControlEvents:UIControlEventTouchUpInside];
    //将按钮向左偏移10个像素
    return [[UIBarButtonItem alloc] initWithCustomView:aCustomView];
}

//生成一个自定义的导航栏按钮
UIBarButtonItem *getCustomNavButton(NSString *iTitle,id iTarget, SEL iSelector)
{
    UIButton *aCustomView = [UIButton buttonWithType:UIButtonTypeCustom];
    [aCustomView setBackgroundImage:[UIImage imageNamed:@"标题按钮背景2"] forState:UIControlStateNormal];
    CGSize size = [iTitle sizeWithFont:aCustomView.titleLabel.font];
    [[aCustomView titleLabel] setFont:[UIFont systemFontOfSize:15]];
    aCustomView.frame = CGRectMake(0, 0, size.width+15, 43);
    [aCustomView setTitle:iTitle forState:UIControlStateNormal];
    [aCustomView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aCustomView addTarget:iTarget action:iSelector forControlEvents:UIControlEventTouchUpInside];
    //将按钮放在一个容器View中，向右偏移10个像素
    UIView *backButtonView = [[UIView alloc] initWithFrame:aCustomView.frame];

    backButtonView.bounds = CGRectOffset(backButtonView.bounds, -10, 0);
    [backButtonView addSubview:aCustomView];
//    return [[UIBarButtonItem alloc] initWithCustomView:aCustomView];
    return [[UIBarButtonItem alloc]initWithCustomView:backButtonView];
}

UIBarButtonItem *customNavButton(id iTarget, SEL iSelector)
{
    UIButton *aCustomView = [UIButton buttonWithType:UIButtonTypeCustom];
    [aCustomView setBackgroundImage:[UIImage imageNamed:@"addCom"] forState:UIControlStateNormal];
    aCustomView.frame = CGRectMake(0, 0, 76, 33);
    [aCustomView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aCustomView addTarget:iTarget action:iSelector forControlEvents:UIControlEventTouchUpInside];
    //将按钮放在一个容器View中，向右偏移10个像素
    UIView *backButtonView = [[UIView alloc] initWithFrame:aCustomView.frame];
    
//    backButtonView.bounds = CGRectOffset(backButtonView.bounds, -10, 0);
    [backButtonView addSubview:aCustomView];
    //    return [[UIBarButtonItem alloc] initWithCustomView:aCustomView];
    return [[UIBarButtonItem alloc]initWithCustomView:backButtonView];
}
UIBarButtonItem *rightNavBtnWithImgName(NSString *imageName,id iTarget, SEL iSelector)
{
    UIImage *image = [UIImage imageNamed:imageName];
    UIButton *aCustomView = [UIButton buttonWithType:UIButtonTypeCustom];
    [aCustomView setBackgroundImage:image forState:UIControlStateNormal];
    aCustomView.frame = CGRectMake(0, 0, image.size.width / 2, image.size.height / 2);
    [aCustomView addTarget:iTarget action:iSelector forControlEvents:UIControlEventTouchUpInside];
    //将按钮放在一个容器View中，向右偏移10个像素
    UIView *backButtonView = [[UIView alloc] initWithFrame:aCustomView.frame];
    
    [backButtonView addSubview:aCustomView];
    //    return [[UIBarButtonItem alloc] initWithCustomView:aCustomView];
    return [[UIBarButtonItem alloc]initWithCustomView:backButtonView];
}

UIBarButtonItem *rightNavButton(id iTarget, SEL iSelector,NSString *imageName)
{
    UIImage *image = [UIImage imageNamed:imageName];
    
    UIButton *aCustomView = [UIButton buttonWithType:UIButtonTypeCustom];
    [aCustomView setBackgroundImage:image forState:UIControlStateNormal];
    aCustomView.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
    [aCustomView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aCustomView addTarget:iTarget action:iSelector forControlEvents:UIControlEventTouchUpInside];
    //将按钮放在一个容器View中，向右偏移10个像素
    UIView *backButtonView = [[UIView alloc] initWithFrame:aCustomView.frame];
    
    //    backButtonView.bounds = CGRectOffset(backButtonView.bounds, -10, 0);
    [backButtonView addSubview:aCustomView];
    //    return [[UIBarButtonItem alloc] initWithCustomView:aCustomView];
    return [[UIBarButtonItem alloc]initWithCustomView:backButtonView];
}

BOOL isBlankString(NSString *string)
{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}
id getObjectFromDic(Class itemClass, NSDictionary *dic)
{
    id item = [[itemClass alloc]init];
    NSArray *properties = [item getPropertyList];
    for (NSString *propertyName in properties) {
        NSString *key = propertyName;
        if ([propertyName isEqualToString:@"Id"]) {
            key = @"id";//因为属性不能叫做id所以把I变成了大写
        }
        [item setValue:[dic objectForKey:key] forKey:propertyName];
    }
    return item;
}

NSString *makeUUID()
{
    CFUUIDRef puuid = CFUUIDCreate(nil);
    CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
    NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);
    return result;
}

NSString *getDeviceIdentifier()
{
    static NSString *DEVICE_ID_KEYCHAIN_KEY = @"DeviceIdKeyChainKey";
    NSString *deviceId = [DataStoreService loadFromKeychain:DEVICE_ID_KEYCHAIN_KEY];
    if (deviceId == nil || deviceId.length == 0) {
        deviceId = makeUUID();
        [DataStoreService saveToKeychain:DEVICE_ID_KEYCHAIN_KEY data:deviceId];
    }
    return deviceId;
}
void DoBackGroundMethod()
{
     MKNetworkEngine *engine=[[MKNetworkEngine alloc] initWithHostName:updateOnlineScore_URL customHeaderFields:nil];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    if (userInfo.currentLoginUserName && userInfo.currentLoginUserName.length>0) {
        [dic setValue:userInfo.userInfo.Id forKey:@"userId"];
    }
    MKNetworkOperation *op = [engine operationWithPath:@"" params:dic httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSLog(@"[operation responseData]-->>%@", [operation responseString]);
        //NSDictionary *json = [operation responseJSON];
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
    }];
    [engine enqueueOperation:op];
}
NSString *md5HexDigest(NSString *password)
{
    const char *original_str = [password UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++)
    {
        [hash appendFormat:@"%02X", result[i]];
    }
    NSString *mdfiveString = [hash lowercaseString];
    
    NSLog(@"Encryption Result = %@",mdfiveString);
    
    return [mdfiveString uppercaseString];
}
